package com.banshuibao.bsb_android.activity;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.banshuibao.bsb_android.Constants;
import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.http.HttpResponse;
import com.banshuibao.bsb_android.http.HttpUtil;
import com.banshuibao.bsb_android.model.AdModel;
import com.banshuibao.bsb_android.util.GetLoginInfo;
import com.banshuibao.bsb_android.util.ShareUtil;
import com.banshuibao.bsb_android.util.WebViewCssUtil;
import com.umeng.analytics.MobclickAgent;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by zyh on 2017/4/17.
 */

public class WebViewActivity extends BaseActivity {

    private WebView webView;

    private String uuid, title, tag;

    private boolean isUrl;

    private WebViewClient client = new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return super.shouldOverrideUrlLoading(view, url);
        }
    };
    private GetLoginInfo getLoginInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_view);
        getLoginInfo = GetLoginInfo.getLoginInfoInstance(this).readUser();
        initData();
        initView();
    }

    private void initData() {
        uuid = getIntent().getStringExtra("uuid");
        title = getIntent().getStringExtra("title");
        tag = getIntent().getStringExtra("tag");
        isUrl = getIntent().getBooleanExtra("isUrl", false);
    }

    private void initView() {
        TextView title_text = findViewById(R.id.title_text);
        title_text.setSelected(true);
        title_text.setText(title);
        webView = findViewById(R.id.webView);
        if (tag.equals("live")) {
            TextView share = findViewById(R.id.menu);
            share.setVisibility(View.VISIBLE);
            share.setText("分享");
            share.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    ShareUtil.showShare(WebViewActivity.this, Constants.Url.FORESHOW + uuid+"&firmId="+getLoginInfo.getFirmId()+"&inviteUserId="+getLoginInfo.getUserId(), title, "",null);
                }
            });
        }
        //webView.getSettings().setJavaScriptEnabled(true);
        //webView.getSettings().setDefaultTextEncodingName("utf-8");
        webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        webView.requestFocus();
        webView.setWebViewClient(client);
        //  webView.loadData(html, "text/html", "UTF-8");
        //解决手机端加载网络页面乱码
        //webView.loadDataWithBaseURL(null, html, "text/html", "utf-8", null);

        back_iv = findViewById(R.id.back_iv);
        back_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (webView.canGoBack() && !isUrl)
                    webView.goBack();
                else
                    finish();
            }
        });
        loadHtml();

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack() && !isUrl) {
            webView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void loadHtml() {
        AdModel.BodyDetail body = new AdModel.BodyDetail();
        body.setUuid(uuid);
        HttpUtil.doPost(Constants.Url.DETAILS_ROTATION, body, new HttpResponse(this) {
            @Override
            public void onResult(Object obj) {
                try {
                    JSONObject jsonObject = new JSONObject((String) obj);
                    String html = jsonObject.optString("data");
                    if (webView != null)
                        if (!isUrl) {
                            webView.loadDataWithBaseURL(null, WebViewCssUtil.setCss(html), "text/html", "utf-8", null);
                        } else {
                            webView.loadUrl(html);
                        }
                } catch (JSONException e){
                    MobclickAgent.reportError(WebViewActivity.this, e);
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (webView != null) {
            //webView.getSettings().setJavaScriptEnabled(false);
            if (webView.getParent() != null) {
                ((ViewGroup) webView.getParent()).removeView(webView);
            }
            webView.removeAllViews();
            webView.clearHistory();
            webView.destroy();
            webView = null;
        }
    }
}
